/* eslint-disable */
import dayjs from 'dayjs'


function isMobile() {
    return $(window).width() < 992
        // const mobile = navigator.userAgent.match(
        //     /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
        // )
        // return mobile != null
}

function DebounceImmediate(func, wait, immediate) {
    let time
    const debounced = function() {
        const context = this
        if (time) clearTimeout(time)
        if (immediate) {
            const callNow = !time
            if (callNow) func.apply(context, arguments)
            time = setTimeout(() => {
                time = null
            }, wait)
        } else {
            time = setTimeout(() => {
                func.apply(context, arguments)
            }, wait)
        }
    }
    return debounced
}

function toThousandslsFilter(num) {
    return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}


function _changeTime(time) {
    var oDate = new Date(time * 1),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear + '-' + getBz(oMonth) + '-' + getBz(oDay) + ' ' + getBz(oHour) + ':' + getBz(oMin) + ':' + getBz(oSen); //拼接时间
    return oTime;
}

function getBz(num) {
    if (parseInt(num) < 10) {
        num = '0' + num;
    }
    return num;
}

function utc2beijing(utc_datetime, format) {
    // 转为正常的时间格式 年-月-日 时:分:秒
    var T_pos = utc_datetime.indexOf('T');
    var Z_pos = utc_datetime.indexOf('Z');
    var year_month_day = utc_datetime.substr(0, T_pos);
    var hour_minute_second = utc_datetime.substr(T_pos + 1, Z_pos - T_pos - 1);
    var new_datetime = year_month_day + " " + hour_minute_second; // 2017-03-31 08:02:06

    // 处理成为时间戳
    timestamp = new Date(Date.parse(new_datetime));
    timestamp = timestamp.getTime();

    // 增加8个小时，北京时间比utc时间多八个时区
    var timestamp = timestamp + 8 * 60 * 60 * 1000;

    var beijing_datetime = _changeTime(timestamp)
        // 时间戳转为时间
    return dayjs(beijing_datetime).format(format); // 2017-03-31 16:02:06
}

export { isMobile, DebounceImmediate, toThousandslsFilter, utc2beijing }